﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ATW.MT.CommonClass;
using ATW.MT.BUS;
using System.Data;
using Newtonsoft.Json;
using System.Web.Script.Serialization;
using ATW.Bus;
using System.Text;

namespace ATW.MT.BaseManage
{
    public partial class ItemManage : CommonPage
    {
        bus_D_Item Item = new bus_D_Item();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserInfo"] == null)
            {
                Server.Transfer("../Main/Login.aspx");
                return;
            }
            if (!IsPostBack)
            {
                BindData();
                initList();
            }
        }

        //数据绑定
        private void BindData()
        {
            try
            {
                string strDiName = txtDiName.Value.Trim();
                DataTable dt = Item.ItemPagerQuery(strDiName, ddlStatus.SelectedValue, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize);
                if (dt != null && dt.Rows.Count > 0)
                {
                    AspNetPager1.RecordCount = Convert.ToInt32(dt.Rows[0]["totalpagesize"]);
                }
                else
                {
                    AspNetPager1.RecordCount = 0;
                }
                rpList.DataSource = dt;
                rpList.DataBind();
            }
            catch (Exception ex)
            {

            }

        }
        //初始化表单
        private void initList()
        {
            try
            {
                ddlCat.Items.Clear();
                DataTable dt = Item.CateQuery();
                if (dt != null && dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        ddlCat.Items.Add(new ListItem(dt.Rows[i]["ticname"].ToString(), dt.Rows[i]["ticid"].ToString()));
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }
        //查询
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            BindData();
        }

        protected void AspNetPager1_pageChanged(object sender, EventArgs e)
        {
            BindData();
        }


        //编辑界面 确认按钮事件
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                string strDiid = "";
                string strcid = "";
                string strName = "";
                string strDesc = "";
                string strUnit = "";
                string level = "";

                //根据主键是否为空，判断为新增或者修改
                if (string.IsNullOrWhiteSpace(txtMDiid.Value))
                {
                    strName = txtName.Value;
                    strDesc = txtDesc.Value;
                    strUnit = txtUnit.Value;
                    strcid = ddlCat.SelectedValue;
                    level = ddlItemLevel.SelectedValue;

                    int result = Item.AddItem(strName, strDesc, strUnit, strcid, level);
                    if (result > 0)
                    {
                        Tips("新增成功");
                        Clear();
                        BindData();
                    }
                    else
                    {
                        Tips("新增失败");
                        return;
                    }
                }
                else
                {
                    strDiid = txtMDiid.Value;
                    strName = txtName.Value;
                    strDesc = txtDesc.Value;
                    strUnit = txtUnit.Value;
                    strcid = ddlCat.SelectedValue;
                    level = ddlItemLevel.SelectedValue;


                    int result = Item.ModifyItem(strDiid, strName, strDesc, strUnit, strcid, level);
                    if (result > 0)
                    {
                        Tips("修改成功");
                        Clear();
                        BindData();
                    }
                    else
                    {
                        Tips("修改失败");
                        return;
                    }
                }


            }
            catch (Exception ex)
            {
                //return "操作失败:" + ex.Message;
            }
        }

        //删除
        protected void lbnDelete_Click(object sender, EventArgs e)
        {
            LinkButton lbn = (LinkButton)sender;
            string strId = lbn.CommandArgument;
            try
            {
                if (!Item.canItemBeDel(strId))
                {
                    Tips("该子项不能被删除！");
                    return;
                }
                int result = Item.UserChangeStatus(strId, "2");
                if (result > 0)
                {
                    Tips("删除成功");
                    BindData();
                }
                else
                {
                    Tips("删除失败");
                }
            }
            catch (Exception ex)
            {

            }
        }

        protected void lbnRecover_Click(object sender, EventArgs e)
        {//恢复
            LinkButton lbn = (LinkButton)sender;
            string strId = lbn.CommandArgument;
            try
            {
                int result = Item.UserChangeStatus(strId, "0");
                if (result > 0)
                {
                    Tips("删除成功");
                    BindData();
                }
                else
                {
                    Tips("删除失败");
                }
            }
            catch (Exception ex)
            {

            }
        }
        //修改
        protected void lbnModify_Click(object sender, EventArgs e)
        {
            LinkButton lbn = (LinkButton)sender;
            string strId = lbn.CommandArgument;
            try
            {
                DataTable dtResult = Item.SelectItem(strId);

                if (dtResult.Rows.Count > 0)
                {
                    this.txtMDiid.Value = dtResult.Rows[0]["DIID"].ToString();
                    this.txtName.Value = dtResult.Rows[0]["diName"].ToString();
                    this.txtDesc.Value = dtResult.Rows[0]["diDesc"].ToString();
                    this.txtUnit.Value = dtResult.Rows[0]["diUnit"].ToString();
                    this.ddlCat.SelectedValue = dtResult.Rows[0]["ticid"].ToString();
                    this.txtMDiid.Visible = false;
                    string level = dtResult.Rows[0]["level"].ToString().Trim();
                    this.ddlItemLevel.SelectedValue = level;
                }

                StringBuilder sb = new StringBuilder();

                sb.Append("<script language='javascript'>");

                sb.Append("var divAdd = new jSharp.popUpDiv('divAdd');function Show(){divAdd.Show()} Show(); $('#hideAdd').click(function () {divAdd.Close();})");

                sb.Append("</script>");

                ClientScript.RegisterStartupScript(this.GetType(), "LoadPicScript", sb.ToString());

            }
            catch (Exception ex)
            {

            }
        }


        private void writeRes(string s)
        {
            Response.Clear();
            Response.Write(s);
            Response.End();
        }
        //清空控件
        private void Clear()
        {
            txtMDiid.Value = "";
            txtName.Value = "";
            txtDesc.Value = "";
            txtUnit.Value = "";
        }
        private void Tips(string s)
        {
            StringBuilder strScript = new StringBuilder();
            strScript.Append(@"<script language=javascript>
                var divTips=new jSharp.popUpDiv('divMsg');
                document.getElementById('msgContent').innerHTML='" + s + @"';
                divTips.Show();
                var t=setTimeout(function(){
                    divTips.Close();
                    clearTimeout(t);
                },1000);
            </script>");
            this.ClientScript.RegisterStartupScript(this.GetType(), "tips", strScript.ToString());
        }
    }
}